﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TestCalc
{
    public partial class GetDataFromMySQL : Form
    {
        public GetDataFromMySQL()
        {
            InitializeComponent();
        }

        private void btnMySQLData_Click(object sender, EventArgs e)
        {
            string strCon = "server = 192.168.1.101; user id = root; password = cally5075; database = test; persist security info = false";
            MySqlConnection conn = new MySqlConnection(strCon);
            MySqlCommand comm = new MySqlCommand();
            MySqlDataReader reader = null;
            comm.Connection = conn;
            comm.CommandText = "select * from lzlj1";
            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(reader);
                dt.Columns[0].ColumnName = "瓶号";
                dataGridView1.DataSource = dt;
               // dataGridView1.Columns.
                //dataGridView1[0, 0].Value = "7";

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("数据库读取错误：" + ex.Message);
            }
            finally
            {
                reader.Close();
                conn.Close();
            }
        }
    }
}
